use strict;
use warnings;
use Module::Build;

use File::Spec;
use SVN::Core;
use SVN::Repos;
use SVN::Fs;


my $class = Module::Build->subclass(
	class => 'MTAppTestBuilder',
	code => q{
		# Don't make blib
		# sub ACTION_code {};
		# Don't make blib
		sub ACTION_docs {};
		# Don't make META.yml
		sub ACTION_distmeta {
			# no warning on ACTION_distdir
			$_[0]->{metafile} = 'MANIFEST';
		};
		# Don't add MEATA.yml to MANIFEST
		sub ACTION_manifest {
			$_[0]->{metafile} = 'MANIFEST',
			$_[0]->SUPER::ACTION_manifest(@_);
		};

		sub subversion_url {
			my ($url) = grep(/^URL/, `svn info`);
			chomp($url);
			$url =~ s/.*URL: *//;
			$url =~ s#(\.googlecode\.com/svn/).*#$1#;
			$url;
		};

		sub ACTION_subversion_tag {
			my ($self) = @_;
			my $url = &subversion_url;
			my $ver = $self->dist_version;

			system('svn commit;');
			system("svn copy ${url}trunk/ ${url}tags/release_${ver}");
		};

		sub google_project {
			my ($url) = grep(/^URL/, `svn info`);
			chomp($url);
			$url =~ s#.*://##;
			$url =~ s#\.googlecode\.com/svn/.*#$1#;
			$url;
		};

		sub ACTION_upload_google {
			my ($self) = @_;
			my $prj = &google_project;
			my $ver = $self->dist_version;
			my $dist_dir = $self->dist_dir;

			$self->depends_on('dist');
			#$self->_call_action('distdir');
			$self->ACTION_distdir;
			$self->depends_on('zipdist');
			system(
				"googlecode_upload.py -s 'Release $ver (TGZ)' -p $prj $dist_dir.tar.gz"
			);
			system(
				"googlecode_upload.py -s 'Release $ver (ZIP)' -p $prj $dist_dir.zip"
			);
			unlink("$dist_dir.tar.gz");
			unlink("$dist_dir.zip");
		};

		sub ACTION_zipdist {
			my ($self) = @_;
			my $dist_dir = $self->dist_dir;
			$self->depends_on('distdir');
			print "Creating $dist_dir.zip\n";
			system("zip -r $dist_dir.zip $dist_dir") == 0 or die $?;
			$self->delete_filetree($dist_dir);
		}
	}
);

my $builder = $class->new(
    module_name         => 'MT::App::Test',
    license             => 'perl',
    dist_author         => 'Taku Amano <taku@toi-planning.net>',
    dist_version_from   => 'lib/MT/App/Test.pm',
    requires => {
		'YAML::Tiny' => 0,
        'version'    => 0,

        'Test::More' => 0,
		'JSON'       => 0,

		'DBD::SQLite' => 0,
		#'DBD::Pg' => 0,
		#'DBD::mysql' => 0,
    },
    add_to_cleanup      => [ 'MT-App-Test-*' ],

	build_elements      => [
		'pm', 'pod', 'cgi', 'yml', 'yaml', 'php',
	],
);

$builder->create_build_script();
